//
//  BandVC.m
//  SSWiFi
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2024/1/25.
//

#import "BandVC.h"
#import "BandItemCell.h"

@interface BandVC ()
@property (strong,nonatomic) NSArray<BandItemModel *> *bands;
@end

@implementation BandVC
static NSString * const bandcell_identifier = @"bandcell_identifier";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.tableView registerNib:[UINib nibWithNibName:@"BandItemCell" bundle:nil] forCellReuseIdentifier:bandcell_identifier];
    [self getSupportBand];
}


- (void)getSupportBand{
    MJWeakSelf;
    [self.api getSupportBandCallBack:^(NSArray<BandItemModel *> * _Nullable bands) {
        weakSelf.bands = bands;
        if(bands.count > 0){
            [weakSelf getSelectedBand];
        }else{
            [self reloadData];
        }
    }];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = [NSString stringWithFormat:@"%@锁频",self.api.devicesName];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithTitle:@"应用" target:self action:@selector(setSelectedBand)];
}

- (void)setSelectedBand{
    NSMutableArray *arr = [NSMutableArray array];
    for(BandItemModel *m in self.bands){
        if(m.value == 1){
            [arr addObject:m.name];
        }
    }
    if(arr.count > 0){
        NSString *bds = [arr componentsJoinedByString:@","];
        
        QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:nil];
        MJWeakSelf;
        QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
            [weakSelf submitSelectedBands:bds];
        }];
       
        
        NSString *msg = [NSString stringWithFormat:@"确定要锁定 %@吗?",bds];
        QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [alertController showWithAnimated:YES];
        
    }else{
        [QMUITips showError:@"请选择频段"];
        return;
    }
}

- (void)submitSelectedBands:(NSString *)bands{
    MJWeakSelf;
    [QMUITips showLoading:@"设置中..." inView:self.view];
    
    [self.api setBandPara:bands callBack:^(bool flag, NSString * _Nonnull err) {
        [QMUITips hideAllTips];
        if(flag){
            [QMUITips showSucceed:@"设置成功,等待设备重启"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if(weakSelf.api.deviceType == WiFiDevice_WC){
                    [weakSelf.api rebootCallback:^(bool flag, NSString * _Nonnull msg) {

                    }];
                }
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            });

        }else{
            [QMUITips showError:err];
        }
    }];
}

- (void)getSelectedBand{
    [self.api getSelectedBandCallBack:^(NSString * _Nullable bands) {
        [self filldata:bands];
    }];
}

- (void)filldata:(NSString *)bandstr{
    NSArray *arr = [bandstr componentsSeparatedByString:@","];
    for(NSString *b in arr){
        for(BandItemModel *d in self.bands){
            if([d.name isEqualToString:b]){
                d.value = 1;
                break;
            }
        }
    }
    [self reloadData];
}

- (void)reloadData{
    [self.tableView reloadData];
    if(self.bands.count == 0){
        [self.emptyView setHidden:NO];
        [self showEmptyViewWithText:@"暂无数据" detailText:nil buttonTitle:@"重试" buttonAction: @selector(getSupportBand)];
    }else{
        [self.emptyView setHidden:YES];
    }
}

- (void)alertNoSupport{
    QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"错误" message:@"该设备暂时不支持锁频" preferredStyle:QMUIAlertControllerStyleAlert];
    [alertController addAction:action1];
    [alertController showWithAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.bands.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    BandItemCell *cell = [tableView dequeueReusableCellWithIdentifier:bandcell_identifier];
    cell.model = self.bands[indexPath.row];
    return cell;
}

@end
